package com.dianwang.utils;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class DefectLibrary {

    public static class Defect {
        private String device;
        private String deviceType;
        private String part;
        private String partType;
        private String position;
        private String defectDescription;
        private String classificationBasis;
        private String defectClassification;

        // 构造函数
        public Defect(String device, String deviceType, String part, String partType, String position,
                      String defectDescription, String classificationBasis, String defectClassification) {
            this.device = device;
            this.deviceType = deviceType;
            this.part = part;
            this.partType = partType;
            this.position = position;
            this.defectDescription = defectDescription;
            this.classificationBasis = classificationBasis;
            this.defectClassification = defectClassification;
        }

        // Getter 方法
        public String getDevice() {
            return device;
        }

        public String getDeviceType() {
            return deviceType;
        }

        public String getPart() {
            return part;
        }

        public String getPartType() {
            return partType;
        }

        public String getPosition() {
            return position;
        }

        public String getDefectDescription() {
            return defectDescription;
        }

        public String getClassificationBasis() {
            return classificationBasis;
        }

        public String getDefectClassification() {
            return defectClassification;
        }
    }

    private List<Defect> defects = new ArrayList<>();

    public DefectLibrary(String excelFilePath) {
        loadDefects(excelFilePath);
    }

    private void loadDefects(String excelFilePath) {
        try (FileInputStream fis = new FileInputStream(excelFilePath);
             Workbook workbook = new XSSFWorkbook(fis)) {

            Sheet sheet = workbook.getSheetAt(0); // 假设数据在第一个工作表
            Iterator<Row> rowIterator = sheet.iterator();

            if (rowIterator.hasNext()) {
                rowIterator.next(); // 跳过表头
            }

            int lineNumber = 1;
            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                lineNumber++;

                // 读取必要的列
                // 假设列顺序为：
                // 0: device
                // 1: device_type
                // 2: part
                // 3: part_type
                // 4: position
                // 5: defect_description
                // 6: classification_basis
                // 7: defect_classification
                String device = getCellValue(row.getCell(0));
                String deviceType = getCellValue(row.getCell(1));
                String part = getCellValue(row.getCell(2));
                String partType = getCellValue(row.getCell(3));
                String position = getCellValue(row.getCell(4));
                String defectDescription = getCellValue(row.getCell(5));
                String classificationBasis = getCellValue(row.getCell(6));
                String defectClassification = getCellValue(row.getCell(7));

                // 判空
                if (device.isEmpty() && deviceType.isEmpty() && part.isEmpty() && partType.isEmpty() &&
                        position.isEmpty() && defectDescription.isEmpty() && classificationBasis.isEmpty() &&
                        defectClassification.isEmpty()) {
                    System.out.println("警告: 第 " + lineNumber + " 行关键字段为空，跳过该行。");
                    continue;
                }

                Defect defect = new Defect(device, deviceType, part, partType, position,
                        defectDescription, classificationBasis, defectClassification);
                defects.add(defect);
            }
        } catch (IOException e) {
            System.out.println("错误: 读取文件时发生异常 " + excelFilePath);
            e.printStackTrace();
        }
    }

    private String getCellValue(Cell cell) {
        if (cell == null) return "";
        switch (cell.getCellType()) {
            case STRING:
                return cell.getStringCellValue().trim();
            case NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    return cell.getDateCellValue().toString();
                } else {
                    double d = cell.getNumericCellValue();
                    if (d == (long) d) {
                        return String.valueOf((long) d);
                    } else {
                        return String.valueOf(d);
                    }
                }
            case BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue());
            case FORMULA:
                return cell.getCellFormula();
            default:
                return "";
        }
    }

    public List<Defect> getDefects() {
        return defects;
    }
}
